package com.haifeng.jupiter.common.core.util;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  国际化工具类
 * </p>
 *
 * @author: Haifeng
 * @date: 2020-08-09
 */
@Slf4j
@Component
@AllArgsConstructor
public class MessageUtils {

    private final MessageSource messageSource;

    /**
     * 获取单个国际化翻译值
     */
    public String get(Integer code, Object ... obj) {
        try {
            String message = messageSource.getMessage(code.toString(), null, LocaleContextHolder.getLocale());
            return StrUtil.format(message, obj);
        } catch (Exception e) {
            return "";
        }
    }
}
